package co.mobiless.noterecorder;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SimpleCursorAdapter;

public class ImageSimpleCursorAdapter  extends SimpleCursorAdapter{
	private Cursor c;
    private Context context;

    
    public ImageSimpleCursorAdapter(Context context, int layout, Cursor c,
			String[] from, int[] to) {
		super(context, layout, c, from, to);
		this.c = c;
		this.context = context;
	}
    public Nota getItem(int position) {
    	Nota nota= new Nota();
    	nota.setTiempoInicio(this.c.getString(this.c.getColumnIndex(StorageData.KEY_ROW_INITIAL_TIME_NOTE)));
    	nota.setTiempoFin(this.c.getString(this.c.getColumnIndex(StorageData.KEY_ROW_LENGHT_NOTE)));
    	nota.setTextoAdicional(this.c.getString(this.c.getColumnIndex(StorageData.KEY_ROW_ADITIONAL_TEXT_NOTE)));
    	nota.setUbicacionImagen(this.c.getString(this.c.getColumnIndex(StorageData.KEY_ROW_LOCATION_IMAGE_NOTE)));
    	nota.setNombreArchivoImagen(this.c.getString(this.c.getColumnIndex(StorageData.KEY_ROW_NAME_ARCHIVE_IMAGE_NOTE)));
    	return nota;        
    }
    public View getView(int pos, View inView, ViewGroup parent) {
	       View v = inView;
	       if (v == null) {
	            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	            v = inflater.inflate(R.layout.fila_notaimagen, null);
	       }
	       this.c.moveToPosition(pos);		
	       //String nombre = this.c.getString(this.c.getColumnIndex(StorageData.KEY_ROW_NAME_NOTE));
	       //String tiempoIni = this.c.getString(this.c.getColumnIndex(StorageData.KEY_ROW_INITIAL_TIME_NOTE));
	       //String textoAdd = this.c.getString(this.c.getColumnIndex(StorageData.KEY_ROW_ADITIONAL_TEXT_NOTE));
	       String rutaImgNota = this.c.getString(this.c.getColumnIndex(StorageData.KEY_ROW_LOCATION_IMAGE_NOTE))+"/"+
	       		this.c.getString(this.c.getColumnIndex(StorageData.KEY_ROW_NAME_ARCHIVE_IMAGE_NOTE));
	       if (rutaImgNota != null) {
	    	   
	    	   Bitmap b = new BitmapFactory().decodeFile(rutaImgNota);
	    	   
	    	   Log.d("Width&Heigth", b.getWidth()+"&"+b.getHeight());
	    	   
	    	   ImageView iv = (ImageView) v.findViewById(R.id.notaminiatura);
	    	   iv.setImageBitmap( Bitmap.createScaledBitmap(b, b.getWidth()/3, b.getWidth()/3, false) );
	    	   //iv.setImageBitmap( BitmapFactory.decodeFile(rutaImgNota ));
	       }
         /*TextView nombreNota = (TextView) v.findViewById(R.id.nombreNota);
         TextView textoAdicional = (TextView) v.findViewById(R.id.textoAdicional);
         TextView tiempoInicial = (TextView) v.findViewById(R.id.initTime);
         nombreNota.setText(nombre);
         textoAdicional.setText(textoAdd.length()>=20?textoAdd.substring(0, 20)+"...":
         	textoAdd==null||textoAdd.length()==0?"--":textoAdd);
         int h = (int) ((Integer.parseInt(tiempoIni) / 1000) / 3600);
         int m = (int) (((Integer.parseInt(tiempoIni) / 1000) / 60) % 60);
         int s = (int) ((Integer.parseInt(tiempoIni) / 1000) % 60);
         tiempoInicial.setText((h<10?"0"+h:h)+":"+(m<10?"0"+m:m)+":"+(s<10?"0"+s:s));*/
	       return(v);
	}
}
